<?php
/**
 * 定制功能
 *
 * @author 孙晓晔
 * @version $Id: Role.class.php 1051 2010-04-07 08:55:38Z sunxy $
 */

require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Customize extends InfoUtil {
	public $tbl = Constant::tbl_customize;

	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',			
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}
	
	function layout($controller, $request) {
		$this->tab($controller, $request);
	}
	
	function tab($controller, $request) {		
		$emp = $_SESSION['id'];
		// $emp = '101';
		
		require_once(dirname(__FILE__) . '/Func.class.php');
		$aa = new Func();
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		
		$data = array();
		while ($row = $db->fetch_array($result)) {
			if ($row['status'] <> '1') {
				continue;
			}
			
			if (!$aa->_valid($row['func'])) {
				continue;
			}
			
			$k = $row['name'];			
			$row = array('cc'=>$row['link'], 'js'=>'');
			
			$data[$k] = $row;
		}
		
		$ret = array();
		while (list($k, $v) = each($data)) {
			if ($this->tree_my($v)) {
				$v['title'] = $k;
				$ret[] = $v;				 
			}
		}

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'info/customize/tab');
		return true;
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/customize/find');
		return true;
	}

	function _where($request) {		
		$name_s = $request['name_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND a.name like '" . $name_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('149') && $this->_audit('150')) {
			return '';
		}

		if ($this->_audit('149')) {
			return " office = '1'";
		}
		
		if ($this->_audit('150')) {
			return " office = '2'";
		}

		$emp = $_SESSION['id'];

		return " oper = '" . $emp . "'";
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/customize/list');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/customize/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = $request['name_c'];
		$content= $request['content'];
		$status = $request['status'];
			
		if (empty($name)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return false;
		}

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, content, status, createtime) VALUES('%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $name, $content, $status);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 编辑
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		$controller->display($request, 'info/customize/edit');
		return true;
	}

	/**
	 * 编辑修改
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
			
		$name = $request['name'];
		$content= $request['content'];
		$orderby = $request['orderby'];
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', content = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $content, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/customize/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$db->query($sql);			
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function emp($controller, $request) {
		$this->pretreat($controller);

		$emp = $request['id'];
		if (empty($emp)) {
			$emp = $_SESSION['id'];
		}
		
		require_once(dirname(__FILE__) . '/Func.class.php');
		$aa = new Func();
		$a = $aa->_list();
				
		$ret = array();
		while (list($k, $row) = each($a)) {			
			$v['cc'] = $row['link'];
			
			if (!$this->tree_my($v)) {
				continue;
			}
			
			if ($this->emp_link($emp, $v['cc'])) {				
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);

		$controller->display($request, 'info/customize/emp');
		return true;
	}

	function emp_link($emp, $link) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND link = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $link);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}

	/**
	 * 定制信息
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {		
		$emp = $request['id'];
		if (empty($emp)) {
			$emp = $_SESSION['id'];
		}
		
		$list = $request['list'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$db->query($sql);		
		if (empty($list)) {
			return false;
		}
		
		$bb = explode(',', $list);
		while (list($k, $v) = each($bb)) {
			$a = explode('|', $v);
			$this->_update($emp, $a[0], $a[1], $a[2]);
		}
		return true;
	}
	
	function _update($emp, $func, $name, $link) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND func = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $func);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$fmt = "UPDATE %s SET status = '1', modifytime = NOW() WHERE emp = '%s' AND link = '%s'";
			$sql = sprintf($fmt, $this->tbl, $emp, $link);
		} else {
			$fmt = "INSERT INTO %s(emp, func, name, link, oper, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $emp, $func, $name, $link, $_SESSION['id']);
		}
		debug($sql);
		$db->query($sql);
	}
	
	function emp_func($emp, $func) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND func = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $func);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}
	
	function _update2($id, $name, $link, $list) {		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = '2' WHERE func = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
		
		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$fmt = "SELECT * FROM %s WHERE emp = '%s' AND func = '%s'";
			$sql = sprintf($fmt, $this->tbl, $emp, $id);
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				$fmt = "UPDATE %s SET status = '1', modifytime = now() WHERE emp = '%s' AND func = '%s'";
				$sql = sprintf($fmt, $this->tbl, $emp, $id);
			} else {
				$fmt = "INSERT INTO %s(emp, func, name, link, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $emp, $id, $name, $link);
			}
			debug($sql);
			$db->query($sql);
		}
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
}
?>